import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
// import vueDevTools from 'vite-plugin-vue-devtools'

// https://vitejs.dev/config/
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd())

  return defineConfig({
    plugins: [vue(), vueJsx()],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    // build: {
    //   outDir: './dist/template',
    //   assetsDir: './assets'
    // },
    base: './',
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true
        }
      }
    },
    define: {
      'process.env.MODE': JSON.stringify(env.MODE),
      'process.env.VITE_API_URL': JSON.stringify(env.VITE_API_URL),
      'process.env.VITE_API_URL_DEV': JSON.stringify(env.VITE_API_URL_DEV)
    },
    server: {
      host: '0.0.0.0',
      port: 51740, // 端口号
      open: false, // 是否自动打开浏览器
      proxy: {
        '^/path': {
          target: env.VITE_API_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/path/, '')
        },
        '^/eprofile-web': {
          target: env.VITE_API_URL_DEV,
          changeOrigin: true
          // rewrite: (path) => path.replace(/^\/eprofile-web/, '')
        }
      }
    }
  })
}
